/**
 * 2018年11月27日下午3:39:27
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.dao.ModelToSql;
import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.SalesManDao;
import com.baihang.project.ds.model.RenChouModel;
import com.baihang.project.ds.model.SalesManModel;

@Service
public class SalesManService extends BaseService {
	@Autowired
	private SalesManDao dao;
	
	public String getGrid(int page,int pagesize,String sortname,String sortorder) {
		int count = dao.queryForInt("select count(*) from saller");
		
		List<Record> list = dao.queryForRecordList(String.format("select * from saller order by %s %s limit %d,%d ", sortname,sortorder,(page-1)*pagesize,pagesize));
		String jsonRecord = JsonKit.listToJson(list, 1);
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"Rows\":"+jsonRecord+",");
		sb.append("\"Total\":"+count);
		sb.append("}");
		return sb.toString();
	}
	public String getRcGrid(int page,int pagesize,String sortname,String sortorder) {
		int count = dao.queryForInt("select count(*) from t_renchou");
		
		List<Record> list = dao.queryForRecordList(String.format("select * from t_renchou order by %s %s limit %d,%d ", sortname,sortorder,(page-1)*pagesize,pagesize));
		String jsonRecord = JsonKit.listToJson(list, 1);
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"Rows\":"+jsonRecord+",");
		sb.append("\"Total\":"+count);
		sb.append("}");
		return sb.toString();
	}
	
	public boolean saveSalesMan(String name, String tel) {
		int count = dao.queryForInt(String.format("select count(*) from saller where D_Tel='%s' ", tel));
		if(count>0) {
			return false;
		}else {
			SalesManModel model = new SalesManModel(name,tel);
			count = dao.save(ModelToSql.modelToSaveSql(model));
			if(count>0) {
				return true;
			}
		}
		return false;
	}
	
	public boolean saveRenChou(String name, String money) {
		int count = dao.queryForInt(String.format("select count(*) from t_renchou where mondey='%s' ", money));
		if(count>0) {
			return false;
		}else {
			RenChouModel model = new RenChouModel();
			model.setName(name);
			model.setMoney(money);
			count = dao.save(ModelToSql.modelToSaveSql(model));			
			if(count>0) {
				return true;
			}
		}
		return false;
	}
	
	public boolean deleteSalesMan(int id) {
		int count = dao.delete(String.format("delete from saller where D_ID=%d", id));
		if(count >0) {
			return true;
		}
		return false;
	}
	public boolean deleteRenChou(int id) {
		int count = dao.delete(String.format("delete from t_renchou where id=%d", id));
		if(count >0) {
			return true;
		}
		return false;
	}
	
}
